package HZTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Hang
 * @date 2022/9/22
 * @project AutumnOffer
 * @description
 **/
public class lc90 {
    public static void main(String[] args) {
        int[] nums = new int[]{1,2,2};
        List<List<Integer>> lists = subsetsWithDup(nums);
        System.out.println(lists);
    }
    public static List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        Arrays.sort(nums);
        boolean[] used = new boolean[nums.length];
        backtrack(res,0,path,nums,used);
        return res;
    }
    public static void backtrack(List<List<Integer>> res,int start,List<Integer> path,int[] nums,boolean[] used){
        res.add(new ArrayList<>(path));
        if (start >= nums.length){
            return;
        }
        for (int i = start; i < nums.length; i++){
            if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]){
                continue;
            }
            path.add(nums[i]);
            used[i] = true;
            backtrack(res,i+1,path,nums, used);
            path.remove(path.size()-1);
            used[i] = false;
        }
    }
}
